//
//  HSNavigationController.m
//  AFNetworking
//
//  Created by huang on 2018/9/29.
//

#import "HSNavigationController.h"
#import "UINavigationBar+Awesome.h"

@interface HSNavigationController ()<UINavigationControllerDelegate>

//记录push标志,防止重复跳转
@property (nonatomic, getter=isPushing) BOOL pushing;

@end

@implementation HSNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.设置代理
    self.delegate = self;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.pushing == YES) {
        return;
    } else {
        self.pushing = YES;
    }

    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }

    [super pushViewController:viewController animated:animated];
}

#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    self.pushing = NO;
    
    [navigationController setNavigationBarHidden:[self navigationController:navigationController shouldHideNavigationBarOfViewController:viewController] animated:animated];
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        if (self.viewControllers.count > 1) {
            self.interactivePopGestureRecognizer.enabled = YES;
            self.interactivePopGestureRecognizer.delegate = nil;
        }else {
            self.interactivePopGestureRecognizer.enabled = NO;
        }
    }
}

#pragma mark - private
- (BOOL)navigationController:(UINavigationController *)navigationController shouldHideNavigationBarOfViewController:(UIViewController *)viewController
{
    for (NSString *vcName in self.viewControllerNeedNavigationBarHiddenNames) {
        if ([vcName isEqualToString:NSStringFromClass([viewController class])]) {
            return YES;
        }
    }
    return NO;
}

#pragma mark - setter or getter
- (void)setNavigationBarLineColor:(UIColor *)navigationBarLineColor
{
    _navigationBarLineColor = navigationBarLineColor;
    
    if (_navigationBarLineColor) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.navigationBar hs_setBottomLineColor:self.navigationBarLineColor];
        });
    }
}


@end
